package com.heze.xunhuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heze.xunhuan.entity.Favorite;
import com.heze.xunhuan.entity.Like;
import com.heze.xunhuan.entity.Post;
import com.heze.xunhuan.exception.BusinessException;
import com.heze.xunhuan.mapper.FavoriteMapper;
import com.heze.xunhuan.mapper.LikeMapper;
import com.heze.xunhuan.service.InteractionService;
import com.heze.xunhuan.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 互动服务实现类
 *
 * @author heze-xunhuan
 */
@Service
@RequiredArgsConstructor
public class InteractionServiceImpl implements InteractionService {

    private final LikeMapper likeMapper;
    private final FavoriteMapper favoriteMapper;
    private final PostService postService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void like(Long userId, Long postId) {
        // 检查信息是否存在
        Post post = postService.getById(postId);
        if (post == null || "deleted".equals(post.getStatus())) {
            throw new BusinessException(404, "信息不存在");
        }

        // 检查是否已点赞
        LambdaQueryWrapper<Like> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Like::getUserId, userId);
        wrapper.eq(Like::getPostId, postId);
        Like existingLike = likeMapper.selectOne(wrapper);

        if (existingLike != null) {
            throw new BusinessException(400, "已经点赞过了");
        }

        // 添加点赞
        Like like = new Like();
        like.setUserId(userId);
        like.setPostId(postId);
        likeMapper.insert(like);

        // 更新点赞数
        post.setLikeCount(post.getLikeCount() + 1);
        postService.updateById(post);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unlike(Long userId, Long postId) {
        LambdaQueryWrapper<Like> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Like::getUserId, userId);
        wrapper.eq(Like::getPostId, postId);
        Like like = likeMapper.selectOne(wrapper);

        if (like == null) {
            throw new BusinessException(400, "还未点赞");
        }

        // 删除点赞
        likeMapper.deleteById(like.getId());

        // 更新点赞数
        Post post = postService.getById(postId);
        if (post != null) {
            post.setLikeCount(Math.max(0, post.getLikeCount() - 1));
            postService.updateById(post);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void favorite(Long userId, Long postId) {
        // 检查信息是否存在
        Post post = postService.getById(postId);
        if (post == null || "deleted".equals(post.getStatus())) {
            throw new BusinessException(404, "信息不存在");
        }

        // 检查是否已收藏
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getUserId, userId);
        wrapper.eq(Favorite::getPostId, postId);
        Favorite existingFavorite = favoriteMapper.selectOne(wrapper);

        if (existingFavorite != null) {
            throw new BusinessException(400, "已经收藏过了");
        }

        // 添加收藏
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setPostId(postId);
        favoriteMapper.insert(favorite);

        // 更新收藏数
        post.setFavoriteCount(post.getFavoriteCount() + 1);
        postService.updateById(post);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unfavorite(Long userId, Long postId) {
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getUserId, userId);
        wrapper.eq(Favorite::getPostId, postId);
        Favorite favorite = favoriteMapper.selectOne(wrapper);

        if (favorite == null) {
            throw new BusinessException(400, "还未收藏");
        }

        // 删除收藏
        favoriteMapper.deleteById(favorite.getId());

        // 更新收藏数
        Post post = postService.getById(postId);
        if (post != null) {
            post.setFavoriteCount(Math.max(0, post.getFavoriteCount() - 1));
            postService.updateById(post);
        }
    }

    @Override
    public Boolean isLiked(Long userId, Long postId) {
        LambdaQueryWrapper<Like> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Like::getUserId, userId);
        wrapper.eq(Like::getPostId, postId);
        return likeMapper.selectCount(wrapper) > 0;
    }

    @Override
    public Boolean isFavorited(Long userId, Long postId) {
        LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Favorite::getUserId, userId);
        wrapper.eq(Favorite::getPostId, postId);
        return favoriteMapper.selectCount(wrapper) > 0;
    }
}

